﻿using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
using Microsoft.VisualStudio.SharePoint;
using System.Windows.Forms;
using CodeCounsel.SharePoint.DevTools.Commands;

namespace CodeCounsel.SharePoint.DevTools.ExplorerNodeExtensions
{
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExtensionNodeTypes.ContentTypeNode)]
    public class ContentTypeNodeExtension
        : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeMenuItemsRequested +=
                delegate(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
                {
                    IMenuItem copyIDMenuItem = e.MenuItems.Add("Copy ID");
                    copyIDMenuItem.Click +=
                        delegate
                        {
                            IContentTypeNodeInfo nodeInfo = e.Node.GetAnnotation<IContentTypeNodeInfo>();
                            string id = e.Node.Context.SharePointConnection.ExecuteCommand<string, string>(CommandIDs.GetContentTypeID, nodeInfo.Name);
                            Clipboard.SetData(DataFormats.Text, id);
                        };
                };
        }
    }
}
